<template>
	
		<view class="detail-content">
			<!-- 背景轮播 -->
			<BgUmage :bgimageList="goodsDetail.imageList"></BgUmage>
			<MainData :goodsDetaul="goodsDetail"></MainData>
			<Monment :uid="uid" 
			:updateComment="getProductComment" 
			:sendComment="sendComment" :moment="monments" />
			<!-- 底部功能 -->
			<Tool 
			:uid="uid"
			:saler-id="goodsDetail.salerId" 
			:goods-id="goodsDetail.id"></Tool>
		</view>
	</scroll-view>

	
</template>

<script>
	import BgUmage from "./components/bg-image.vue";
	import MainData from "./components/main-dtata.vue";
	import Monment from "@/components/comment.vue"
	import Tool from "./components/tool.vue"
	import {getItem} from "@/utils/selfLocalStorage.js"
	import { getGoodsDetailApi } from "@/http/goods.js"
	import { sendComment,getCommentApi } from "@/http/comment.js"
	
	export default {
		data() {
			return {
				uid:null,
				goodsId:null,
				buyerId: 0, // 卖家id
				goodsDetail: {
					id:null,
					bgimageList: []
				},
				commentsQuery:{
					page:0,
					pageSize:10,
					goodsId:null
				},
				total:0,
				monments: []
			}
		},
		components: {
			BgUmage,
			MainData,
			Monment,
			Tool
		},
		onLoad(e) {
			console.log(e.id);
			this.commentsQuery.goodsId = e.id;
			this.goodsId = e.id;
			// 获取商品信息
			this.getGoodsDetail(e.id);
			// 获取评论数据
			this.getCommentList();
			//  添加goodsId
			// this.goodsIds.push(this.goodsDetail.goodsId);
		},
		onShow(e) {
			// 查看是否登录
			this.uid = getItem("uid");
		},
		methods: {
			// 获取商品数据
			async getGoodsDetail(id){
			  const res = await getGoodsDetailApi(id);	
			  this.goodsDetail = res.goods;
			  this.buyerId = res.goods.sellerId;
			},
			
			// 获取评论数据
			async getCommentList(){
				
				const res = await getCommentApi(this.commentsQuery)
				this.monments = res.records;
			},
			
			// 发表评论
			async sendComment(text, toId, subIdex, goodId) {
				console.log(goodId)
				
				const form = {
					userId:this.uid,
					goodsId:this.goodsId,
					username:getItem("username"),
					avatar:getItem("avatar"),
					sort:1,
					data:text,
					parent:toId
				}
			   const res = await sendComment(form);	
			   if(res){
				   // 添加评论
				   if(!form.parent){
					   // 一级评论
					  form["comment"] = []
					  form["createTime"] = Date.now();
					  this.monments.unshift(form);
				   }else{
					   // 二级评论
					    form["createTime"] = Date.now();
					   this.monments[subIdex].comment.unshift(form);
				   }
			   }
			},
			getProductComment() {
				console.log("获取评论")
			}
		}
	}
</script>

<style lang="less" scoped>
	.detail-content {
		width: 100%;
		// height: 100%;
		background-color: #fbf9ec;


	}
</style>